﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;
using System.Web.UI.HtmlControls;

namespace BladeTecWebApplication.Admin
{
	public partial class PrintCompareApplicants : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			NetSysData.NetSysData data;
			string attempt_ids = "";
			string[] ids;
			
			try
			{
				attempt_ids = Request.QueryString["attempt_ids"];
				ids = attempt_ids.Split(':');
			}
			catch (Exception)
			{
				// show error
				return;
			}

			data = new NetSysData.NetSysData();
			
			QUESTION_LEVEL[] levels = data.GetQuestionLevels();
			HiddenField hf = new HiddenField();
			hf.ID = "hfLevels";
			hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
			foreach (QUESTION_LEVEL level in levels)
			{
				hf.Value += level.LEVEL_DESCRIPTION + "|";
			}
			pnlHiddenData.Controls.Add(hf);

			QUESTION_CATEGORY[] categories = data.GetQuestionCagetories();
			hf = new HiddenField();
			hf.ID = "hfCategories";
			hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
			foreach (QUESTION_CATEGORY category in categories)
			{
				hf.Value += category.CATEGORY_NAME + "|";
			}
			pnlHiddenData.Controls.Add(hf);

			GetAttemptList_Result[] attempts = data.GetAttemptList();
			int i = 0;
			foreach (string id in ids)
			{
				int attempt_id = 0;

				try
				{
					attempt_id = Int32.Parse(id);
				}
				catch (Exception) { }

				GetApplicantLevelStatistics_Result[] stats = data.GetApplicantLevelStatics(attempt_id);
				GetQuizLevelStatistics_Result[] quiz_stats = data.GetQuizLevelStatistics(attempt_id);

				GetApplicantCategoryStatistics_Result[] cat_stats = data.GetApplicantCategoryStatistics(attempt_id);
				GetQuizCategoryStatistics_Result[] quiz_cat_stats = data.GetQuizCategoryStatistics(attempt_id);

				QUIZ_ATTEMPT qa = data.GetQuizAttempt(attempt_id);
				if (qa == null)
				{
					continue;
				}

				foreach (GetAttemptList_Result result in attempts)
				{
					if (result.ATTEMPT_ID == attempt_id)
					{
						addApplicantData(qa.APPLICANT, result, i);
						break;
					}
				}
				
				hf = new HiddenField();
				hf.ID = "attemptLevelInfo" + attempt_id;
				hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;

				hf.Value = qa.APPLICANT.FIRST_NAME + " " + qa.APPLICANT.LAST_NAME + "|";

				foreach (GetApplicantLevelStatistics_Result result in stats)
				{
					hf.Value += result.CORRECT_COUNT + ",";
				}
				pnlHiddenData.Controls.Add(hf);

				hf = new HiddenField();
				hf.ID = "attemptCategoryInfo" + attempt_id;
				hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;

				hf.Value = qa.APPLICANT.FIRST_NAME + " " + qa.APPLICANT.LAST_NAME + "|";
				foreach (GetApplicantCategoryStatistics_Result result in cat_stats)
				{
					if (result != null)
					{
						hf.Value += result.CORRECT_COUNT + ",";
					}
				}
				pnlHiddenData.Controls.Add(hf);

				// Now add level and category info.
				hf = new HiddenField();
				hf.ID = "quizLevelInfo";
				hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;

				foreach (GetQuizLevelStatistics_Result result in quiz_stats)
				{
					hf.Value += result.QUESTION_COUNT + ",";
				}
				pnlHiddenData.Controls.Add(hf);

				hf = new HiddenField();
				hf.ID = "quizCategoryInfo";
				hf.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;

				foreach (GetQuizCategoryStatistics_Result result in quiz_cat_stats)
				{
					if (result != null)
					{
						hf.Value += result.QUESTION_COUNT + ",";
					}
				}
				pnlHiddenData.Controls.Add(hf);

				i++;
			}

		}

		private void addApplicantData(APPLICANT applicant, GetAttemptList_Result result, int index)
		{
			HtmlTableCell cell = new HtmlTableCell();
			double grade;

			string div_color = (index % 2 == 0) ? "#CCC" : "#DC291E";

			cell.InnerHtml += "<table>";
			cell.InnerHtml += "<tr>";
			cell.InnerHtml += "<td>Applicant Name:</td>";
			cell.InnerHtml += "<td style=\"font-weight:bold;\">" + applicant.FIRST_NAME + " " + applicant.LAST_NAME + "&nbsp;&nbsp;<span style=\"width:10px; background-color: "+div_color+";border: 1px solid #666;\">&nbsp;&nbsp;</span></td>";
			cell.InnerHtml += "</tr>";

			cell.InnerHtml += "<tr>";
			cell.InnerHtml += "<td>Attempt Date:</td>";
			cell.InnerHtml += "<td>" + DateTime.Parse(result.ATTEMPT_DATE.ToString()).ToString("d") + "</td>";
			cell.InnerHtml += "</tr>";

			cell.InnerHtml += "<tr>";
			cell.InnerHtml += "<td>Graded:</td>";
			cell.InnerHtml += "<td>" + ((result.GRADED == 1) ? "Yes" : "No") + "</td>";
			cell.InnerHtml += "</tr>";

			grade = Double.Parse(result.CORRECT.ToString()) / (Double.Parse(result.CORRECT.ToString()) + Double.Parse(result.INCORRECT.ToString()));
			grade = grade * 100;
			if (grade.ToString() == "NaN")
				grade = 0;
			
			cell.InnerHtml += "<tr>";
			cell.InnerHtml += "<td>Score:</td>";
			cell.InnerHtml += "<td>" + grade.ToString() + "%</td>";
			cell.InnerHtml += "</tr>";
			
			cell.InnerHtml += "</table>";
			cell.Style.Add("padding-right", "25px");

			tblApplicantInfo.Rows[0].Cells.Add(cell);
		}

	}
}